import org.apache.spark.{SparkConf, SparkContext}

object scala4215 {
  def main(args: Array[String]): Unit = {
    // 创建Spark配置
    val conf = new SparkConf()
      .setAppName("IntersectionExample")
      .setMaster("local[*]")
    // 创建SparkContext
    val sc = new SparkContext(conf)

    try {
      // 创建RDD
      val c_rdd1 = sc.parallelize(List(('a', 1), ('a', 1), ('b', 1), ('c', 1)))
      val c_rdd2 = sc.parallelize(List(('a', 1), ('b', 1), ('d', 1)))
      // 使用intersection()方法求出两个RDD的共同元素
      val result = c_rdd1.intersection(c_rdd2).collect()
      println("两个RDD的共同元素:")
      result.foreach(println)
    } catch {
      case e: Exception =>
        println(s"执行过程中出现异常: ${e.getMessage}")
    } finally {
      // 关闭SparkContext
      sc.stop()
    }
  }
}